package likou.jindian;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/12/17 06:58
 */
public class _04_12 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(0);
        treeNode.left = new TreeNode(1);
        treeNode.right = new TreeNode(1);
        System.out.println(pathSum(treeNode,0));
    }
    static int daan = 0;
    static boolean p = true;
    static Deque<TreeNode> nodes = new LinkedList<>();
    public static int pathSum(TreeNode root, int sum) {
        if(root==null){
            return 0;
        }
        nodes.offerLast(root);
        while(nodes.size()>0){
            TreeNode nowNode = nodes.pollFirst();
            if(nowNode.val==sum){
                ++daan;
            }
            deep(nowNode.left,nowNode.val,sum);
            deep(nowNode.right,nowNode.val,sum);
            p=false;
        }
        return daan;
    }
    public static void deep(TreeNode root,int now,int sum){
        if(root==null){
            return;
        }
        if(p){
            nodes.offerLast(root);
        }
        now+=root.val;
        if(now==sum){
            ++daan;
        }
        deep(root.left,now,sum);
        deep(root.right,now,sum);
    }
}
